package Q937

import java.lang.StringBuilder
import java.nio.charset.CharacterCodingException
import java.util.*

/**
 * 重新排列日志文件
 * @author 23737
 * @date 2022.5.5
 */
class Main {

}

fun main() {
    println("[ -><- ]")
    println(" ")
}

/*
 class Solution {
    fun reorderLogFiles(logs: Array<String>): Array<String> {
        Arrays.sort(logs) { log1: String, log2: String ->
            //将log1 按分隔符“ ” ，分成2份，即把标识符分开来
            val split1 = log1.split(" ".toRegex(), 2).toTypedArray()
            val split2 = log2.split(" ".toRegex(), 2).toTypedArray()
            val isDigit1 = Character.isDigit(split1[1][0])
            val isDigit2 = Character.isDigit(split2[1][0])
            //如果两个日志都是字母日志
            if (!isDigit1 && !isDigit2) {
                //先比较内容字母split1>split2则返回1，等于返0，小于返-1
                val cmp = split1[1].compareTo(split2[1])
                if (cmp != 0) return@sort cmp
                //若内容字母相同则比较标识符
                return@sort split1[0].compareTo(split2[0])
            }
            if (isDigit1) if (isDigit2) 0 else 1 else -1
        }
        return logs
    }
}
*/

//这题主要考虑对数组的sortAPI的理解和运用
class Solution {
    fun reorderLogFiles(logs: Array<String>): Array<String> {
        Arrays.sort(logs) { log1: String, log2: String ->
            var split1 = log1.split(" ".toRegex(), 2).toTypedArray()
            var split2 = log2.split(" ".toRegex(), 2).toTypedArray()
            val isDigit1 = Character.isDigit(split1[1][0])
            val isDigit2 = Character.isDigit(split2[1][0])
            if(!isDigit1 && !isDigit2){
                var compareTo = split1[1].compareTo(split2[1])
                if(compareTo != 0){
                    return@sort compareTo
                }
                return@sort split1[0].compareTo(split1[0])
            }
            if (isDigit1) {
                if (isDigit2){
                    0
                } else 1
            } else{
                -1
            }
        }
        return logs
    }
}
